// function golblename(a)
// {
// 	var name= a

// 	return name;
// }



var app = new Vue({
	el: '#app',
	data: {
		//获取的用户列表，用于显示在前端
		userList: [],
		modal2: false,
		modal_loading: false,
		userId: '',
		url: 'http://114.215.83.227:809/admin/',
		userStatus: [{
				value: '已认证',
				label: '已认证'
			},
			{
				value: '未认证',
				label: '未认证'
			},
		],
		selectStatus: [],
		statusUserId: '',
		UserCount: 0,

		//接收筛选条件
		CanditateDate: '',
		CanditateState: 0,
		inputWorkNum: 2018250317,
		init_page: 1,
		img_path: 'aa'


	},
	methods: {
		//时间戳转化
		timestampToTime(cjsj) {
			var date = new Date(date);
			var date = new Date(cjsj) //时间戳为10位需*1000，时间戳为13位的话不需乘1000
			var Y = date.getFullYear() + '-'
			var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
			var D = date.getDate() + ' '
			var h = date.getHours() + ':'
			var m = date.getMinutes() + ':'
			var s = date.getSeconds()
			return Y + M + D + h + m + s
			//console.log(timestampToTime(1533293827000))
		},


		//获取筛选条件
		getCanditate() {
			var vm = this;
			console.log("getCanditate：" + this.CanditateDate)
			if (vm.CanditateDate == "未认证") {

				vm.CanditateState = 1

			} else if (vm.CanditateDate == "已认证") {

				vm.CanditateState = 2
				//console.log("auditState2："+auditState)
			}
			console.log("getCanditate1：" + vm.CanditateState)


		},
		//获取图片 返回一个blob类型的字符串
		getimg(url) {
			var vm = this;
			// url =
			// 	'http://114.215.83.227:809/userImg/2018250027/workImg/2bfac844-df59-4496-a7af-e2ab3b9f120c_2000 (3).jpg'
			var blob_url = '';
			var access_header = 'a' + sessionStorage.getItem("token"); //获取数据token
			axios({
				method: 'get',
				url: url, // 请求地址
				headers: {
					'access_header': access_header
				},
				responseType: 'blob' // 设置接收格式为blob格式
			}).then(res => {
				// console.log(res)
				if (res && res.data && res.data.size) {
					const dataInfo = res.data
					let reader = new window.FileReader()
					// 使用readAsArrayBuffer读取文件, result属性中将包含一个 ArrayBuffer 对象以表示所读取文件的数据
					reader.readAsArrayBuffer(dataInfo)
					reader.onload = function(e) {
						const result = e.target.result
						const contentType = dataInfo.type
						// 生成blob图片,需要参数(字节数组, 文件类型)
						const blob = new Blob([result], {
							type: contentType
						})
						// 使用 Blob 创建一个指向类型化数组的URL, URL.createObjectURL是new Blob文件的方法,可以生成一个普通的url,可以直接使用,比如用在img.src上
						const url = window.URL.createObjectURL(blob)
						vm.img_path = url
						console.log(
							vm.img_path) // 产生一个类似 blob:d3958f5c-0777-0845-9dcf-2cb28783acaf 这样的URL字符串

							
					}
				}
			});
			return vm.img_path;
		},

		//获取页码 并刷新数据
		backPageNum(page) {
			var vm = this;
			//location.reload(); //刷新页面
			if (page > 1) {
				vm.init_page = page

			}
			var access_header = 'a' + sessionStorage.getItem("token"); //获取数据token
			this.$http.post(vm.url + 'adminGetUserList', {
				'currentPageNo': vm.init_page,
				'auditState': vm.CanditateState
			}, {
				emulateJSON: true,
				headers: {
					'access_header': access_header,

				},



			}).then(function(res) {
				if (res.data.status == 1) {

					vm.$Message.success('刷新成功')
					//console.log('获取到的是:' + res.data.message)


					vm.userList = res.data.message
					// golblename(vm.userList)

					// console.log('获取到的是9999:' + golblename())

					for (var i = 0; i < vm.userList.length; i++) {
						// 判断认证状态
						switch (vm.userList[i].auditState) {
							case 1:
								vm.userList[i].auditState = "未认证"
								break;
							case 2:
								vm.userList[i].auditState = "已认证"
								break;
							default:
								break;
						}

						//判断校区campus
						switch (vm.userList[i].campus) {
							case 1:
								vm.userList[i].campus = "未知"
							case 2:
								vm.userList[i].campus = "老校区"
							case 3:
								vm.userList[i].campus = "新校区"
								break;
							default:
								break;
						}

						// 判断logicDelete 逻辑删除
						switch (vm.userList[i].logicDelete) {
							case 1:
								vm.userList[i].logicDelete = "可以登录"
							case 2:
								vm.userList[i].logicDelete = "不能登录"
								break;
							default:
								break;
						}

						// 判断userRole
						switch (vm.userList[i].userRole) {
							case 1:
								vm.userList[i].userRole = "其他"

							case 2:
								vm.userList[i].userRole = "老师"

							case 3:
								vm.userList[i].userRole = "学生"
								break;
							default:
								break;
						}
						//时间戳转化
						vm.userList[i].registerTime = vm.timestampToTime(vm.userList[i].registerTime)


						//console.log('111获取到的是:' + vm.userList[i].userRole)
						
						
						
						// //var b ='http://114.215.83.227:809/userImg/2018250027/workImg/2bfac844-df59-4496-a7af-e2ab3b9f120c_2000 (3).jpg'
						// 			var a = vm.getimg().then(res => {
						//       console.log(res);
						// });
						// // console.log('vm.getimg:'+a)
						//var b ='http://114.215.83.227:809/userImg/2018250027/workImg/2bfac844-df59-4496-a7af-e2ab3b9f120c_2000 (3).jpg'
						// var empty_url = vm.getimg()
						// var a = 0;
						// if(a == 'aa')
						// {
						// 	var empty_url = 
						// }else{
						// 	console.log('vm.getimg:'+ vm.getimg())
						
						// }
						
						vm.userList[i].workCardImg = vm.getimg(vm.userList[i].workCardImg)
						
						
						


					}
						console.log('vm.getimg9999:'+ vm.userList[0].workCardImg)
						console.log('vm.getimg9999:'+ vm.userList[1].orkCardImg)
				} else if (res.data.status == 0) {

					vm.$Message.error('登录失效，3秒后强制登录....')


					setTimeout(() => {

						window.top.location = "login.html"
					}, 3000)

				} else {
					vm.$Message.error('不可预知错误')
				}

			}, function(res) {
				vm.$Message.error('不可预知错误,联系管理员')
			});


		},
		//获取总的用户数量
		getUserSum() {
			var vm = this;
			var access_header = 'a' + sessionStorage.getItem("token"); //获取数据token
			this.$http.post(vm.url + 'adminGetUserCount', {
				// 'currentPageNo': 1
			}, {
				emulateJSON: true,
				headers: {
					'access_header': access_header
				}


			}).then(function(res) {
				if (res.data.status == 1) {

					//vm.$Message.success('刷新成功999')
					//console.log('获取到的是9999:' + res.data.message.countIsState)
					// vm.userList = res.data.message

					vm.UserCount = res.data.message.countIsState




				} else if (res.data.status == 0) {

					//vm.$Message.error('获取失败，请重新登录')

				} else {
					//vm.$Message.error('不可预知错误')
				}

			}, function(res) {
				//vm.$Message.error('不可预知错误,联系管理员')
			});
		},




		//获取id用于修改状态
		getUserId(id) {
			this.statusUserId = id
			console.log("id：" + this.statusUserId)
		},

		// 修改用户认证状态
		changeStatus(index) {
			var vm = this
			// console.log("changeStatusindex："+index)
			// console.log("111111statusUserId："+vm.statusUserId)
			//    console.log("changeStatusindex："+vm.selectStatus[index])

			if (vm.selectStatus[index] == "未认证") {

				var auditState = 1

			} else if (vm.selectStatus[index] == "已认证") {

				var auditState = 2
				//console.log("auditState2："+auditState)
			}

			var access_header = 'a' + sessionStorage.getItem("token"); //获取数据token
			// setTimeout(() => {
			this.$http.post(vm.url + 'adminChangeUserAuditStateByUserId', {
				'userId': vm.statusUserId,
				'auditState': auditState
			}, {
				emulateJSON: true,
				headers: {
					'access_header': access_header
				}
			}).then(function(res) {
				if (res.data.status == 1) {
					//vm.getUserList()
					console.log('111获取到的是:' + res.data.prompt)

					this.$Message.success('修改用户认证状态成功');



				} else if (res.data.status == 0) {

					vm.$Message.error('失败')

					//console.log('111获取到的是:' + res.data.message)

				} else {
					//vm.$Message.error('不可预知错误')
				}

			}, function(res) {
				//vm.$Message.error('不可预知错误,联系管理员')
			});
			// }, 5000);
		},

		//根据工号查询一条
		searchForWorkNum() {
			var vm = this;

			// console.log("changeStatusindex："+index)
			var access_header = 'a' + sessionStorage.getItem("token"); //获取数据token
			this.$http.post(vm.url + 'adminGetUserByWorkNum', {
				'workNum': vm.inputWorkNum
			}, {
				emulateJSON: true,
				headers: {
					'access_header': access_header
				}
			}).then(function(res) {
				if (res.data.status == 1) {

					vm.$Message.success('刷新成功')
					console.log('获取到的是searchForWorkNum:' + res.data.message.workNum)
					vm.userList.length = 0;
					var messages = []
					messages.push(res.data.message);
					console.log(messages);


					vm.userList = messages;




					var i = 0;
					// for (var i = 0; i < vm.userList.length; i++) {
					// 判断认证状态
					switch (vm.userList[i].auditState) {
						case 1:
							vm.userList[i].auditState = "未认证"
							break;
						case 2:
							vm.userList[i].auditState = "已认证"
							break;
						default:
							break;
					}

					//判断校区campus
					switch (vm.userList[i].campus) {
						case 1:
							vm.userList[i].campus = "未知"
						case 2:
							vm.userList[i].campus = "老校区"
						case 3:
							vm.userList[i].campus = "新校区"
							break;
						default:
							break;
					}

					// 判断logicDelete 逻辑删除
					// switch (vm.userList[i].logicDelete) {
					// 	case 1:
					// 		vm.userList[i].logicDelete = "可以登录"
					// 	case 2:
					// 		vm.userList[i].logicDelete = "不能登录"
					// 		break;
					// 	default:
					// 		break;
					// }

					// 判断userRole
					switch (vm.userList[i].userRole) {
						case 1:
							vm.userList[i].userRole = "其他"

						case 2:
							vm.userList[i].userRole = "老师"

						case 3:
							vm.userList[i].userRole = "学生"
							break;
						default:
							break;
					}
					//时间戳转化
					vm.userList[i].registerTime = vm.timestampToTime(vm.userList[i].registerTime)
					console.log('111获取到的是:' + vm.userList[0].userRole)
				} else if (res.data.status == 0) {

					vm.$Message.error('登录失效，3秒后强制登录....')


					setTimeout(() => {

						window.top.location = "login.html"
					}, 3000)

				} else {
					vm.$Message.error('不可预知错误')
				}

			}, function(res) {
				console.log('res:' + res.data.status)
				if (res.data.status == 500 || res.data.status == 400) {

					vm.$Message.error('请输入正确工号....')

				} else {
					vm.$Message.error('未知错误....')
				}




			});



		},


		//通过页数获取被逻辑删除了的用户列表
		showDeledUser(page) {
			var vm = this;

			if (page > 1) {
				vm.init_page = page

			}

			// console.log("changeStatusindex："+index)
			var access_header = 'a' + sessionStorage.getItem("token"); //获取数据token
			this.$http.post(vm.url + 'adminGetLogicDeleteUserList', {
				'currentPageNo': vm.init_page,
			}, {
				emulateJSON: true,
				headers: {
					'access_header': access_header
				}
			}).then(function(res) {
				if (res.data.status == 1) {

					//vm.$Message.success('刷新成功')
					//console.log('获取到的是showDeledUser:' + res.data.message[0].workNum)
					vm.userList.length = 0;
					vm.userList = res.data.message;
					if (vm.userList.length == 0) {
						vm.$Message.error('没有用户....')
					} else {
						var i = 0;
						// for (var i = 0; i < vm.userList.length; i++) {
						// 判断认证状态
						switch (vm.userList[i].auditState) {
							case 1:
								vm.userList[i].auditState = "未认证"
								break;
							case 2:
								vm.userList[i].auditState = "已认证"
								break;
							default:
								break;
						}

						//判断校区campus
						switch (vm.userList[i].campus) {
							case 1:
								vm.userList[i].campus = "未知"
							case 2:
								vm.userList[i].campus = "老校区"
							case 3:
								vm.userList[i].campus = "新校区"
								break;
							default:
								break;
						}

						// 判断logicDelete 逻辑删除
						// switch (vm.userList[i].logicDelete) {
						// 	case 1:
						// 		vm.userList[i].logicDelete = "可以登录"
						// 	case 2:
						// 		vm.userList[i].logicDelete = "不能登录"
						// 		break;
						// 	default:
						// 		break;
						// }

						// 判断userRole
						switch (vm.userList[i].userRole) {
							case 1:
								vm.userList[i].userRole = "其他"

							case 2:
								vm.userList[i].userRole = "老师"

							case 3:
								vm.userList[i].userRole = "学生"
								break;
							default:
								break;
						}
						//时间戳转化
						vm.userList[i].registerTime = vm.timestampToTime(vm.userList[i].registerTime);


					}












					//console.log('showDeledUser:' + vm.userList[0].userRole)
				} else if (res.data.status == 0) {

					vm.$Message.error('登录失效，3秒后强制登录....')


					setTimeout(() => {

						window.top.location = "login.html"
					}, 3000)

				} else {
					vm.$Message.error('不可预知错误')
				}

			}, function(res) {
				//vm.$Message.error('不可预知错误,联系管理员')
			});


		},

		//获取id用于删除
		userDel(id) {
			console.log('enter:' + id)
			this.userId = id;
		},

		//执行删除用户
		del() {

			this.modal_loading = true;
			setTimeout(() => {
				this.modal_loading = false;
				this.modal2 = false;
				//this.$Message.success('删除成功');
			}, 1000);
			// console.log('获取到的是:' + id)

			var vm = this;
			var access_header = 'a' + sessionStorage.getItem("token"); //获取数据token
			this.$http.post(vm.url + 'adminDelUserByUserId', {
				'userId': vm.userId,
			}, {
				emulateJSON: true,
				headers: {
					'access_header': access_header
				}
			}).then(function(res) {
				if (res.data.status == 1) {
					vm.backPageNum();
					console.log('111获取到的是:' + vm.userList[i].userRole)


				} else if (res.data.status == 0) {

					vm.$Message.error('删除失败')

				} else {
					//vm.$Message.error('不可预知错误')
				}

			}, function(res) {
				//vm.$Message.error('不可预知错误,联系管理员')
			});

		},
		//根据userid查询用户电话号码   userIdPhone =  userid 
		ByUserIdTel(userIdPhone, name) {
			sessionStorage.removeItem("userIdPhone"); //删除数据
			var vm = this;
			//console.log('start:' + userIdPhone)
			// var params={
			// 	'userId':userIdPhone,
			// 	'name':name,

			// }
			// console.log('params:'+params.userId)


			sessionStorage.setItem("userIdPhone", userIdPhone); //存储数据
			sessionStorage.setItem("name", name); //存储数据
		},
	},
	mounted() {
		this.backPageNum()
		this.getUserSum()
		this.showDeledUser()

		//this.getimg()
		//this.ByUserIdTel()


	},
	created() {


	},
	watch: {

	}


});
